<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Nodejs之基础API（三） | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,Node,">
  

  <meta name="description" content="Path 提供了一些工具函数，用于处理文件与目录的路径  basename、dirname与extname  返回一个 path的最后一部分 返回一个 path 的目录名 返回 path 的扩展名  const &amp;#123;basename,dirname,extname&amp;#125; = require(&quot;path&quot;);const filePath = &quot;/usr/local/bin/test.t">
<meta name="keywords" content="JavaScript,Node">
<meta property="og:type" content="article">
<meta property="og:title" content="Nodejs之基础API（三）">
<meta property="og:url" content="http://blog.poetries.top/2017/10/15/node-base-api/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="Path 提供了一些工具函数，用于处理文件与目录的路径  basename、dirname与extname  返回一个 path的最后一部分 返回一个 path 的目录名 返回 path 的扩展名  const &amp;#123;basename,dirname,extname&amp;#125; = require(&quot;path&quot;);const filePath = &quot;/usr/local/bin/test.t">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Nodejs之基础API（三）">
<meta name="twitter:description" content="Path 提供了一些工具函数，用于处理文件与目录的路径  basename、dirname与extname  返回一个 path的最后一部分 返回一个 path 的目录名 返回 path 的扩展名  const &amp;#123;basename,dirname,extname&amp;#125; = require(&quot;path&quot;);const filePath = &quot;/usr/local/bin/test.t">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Path"><span class="toc-text">Path</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Buffer"><span class="toc-text">Buffer</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#event"><span class="toc-text">event</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#fs"><span class="toc-text">fs</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#异步解决方案"><span class="toc-text">异步解决方案</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-node-base-api" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">Nodejs之基础API（三）</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2017.10.15</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Back-end/">Back-end</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 2.1k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 10分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="Path"><a href="#Path" class="headerlink" title="Path"></a>Path</h2><blockquote>
<p>提供了一些工具函数，用于处理文件与目录的路径</p>
</blockquote>
<p><strong>basename、dirname与extname</strong></p>
<ul>
<li>返回一个 <code>path</code>的最后一部分</li>
<li>返回一个 <code>path</code> 的目录名</li>
<li>返回 <code>path</code> 的扩展名</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;basename,dirname,extname&#125; = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> filePath = <span class="string">"/usr/local/bin/test.txt"</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(basename(filePath)); <span class="comment">// test.txt</span></span><br><span class="line"><span class="built_in">console</span>.log(dirname(filePath)); <span class="comment">// /usr/local/bin</span></span><br><span class="line"><span class="built_in">console</span>.log(extname(filePath)); <span class="comment">// .txt</span></span><br></pre></td></tr></table></figure>
<p><strong>join、normalize与resolve</strong></p>
<ul>
<li><code>path.join()</code>方法使用平台特定的分隔符把全部给定的 <code>path</code> 片段连接到一起，并规范化生成的路径</li>
<li><code>path.normalize()</code> 方法会规范化给定的 <code>path</code>，并解析 <code>&#39;..&#39;</code>和 <code>&#39;.&#39;</code>片段</li>
<li><code>path.resolve()</code> 方法会把一个相对路径解析为一个绝对路径</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">path.join(<span class="string">'/foo'</span>, <span class="string">'bar'</span>, <span class="string">'baz/asdf'</span>, <span class="string">'quux'</span>, <span class="string">'..'</span>);</span><br><span class="line"><span class="comment">// 返回: '/foo/bar/baz/asdf'</span></span><br><span class="line"></span><br><span class="line">path.join(<span class="string">'foo'</span>, &#123;&#125;, <span class="string">'bar'</span>);</span><br><span class="line"><span class="comment">// 抛出 'TypeError: Path must be a string. Received &#123;&#125;'</span></span><br></pre></td></tr></table></figure>
<p><strong>path.format与path.parse</strong></p>
<ul>
<li><code>path.format()</code>方法会从一个对象返回一个路径字符串。 与 <code>path.parse()</code> 相反</li>
<li><code>path.parse()</code>方法返回一个对象，对象的属性表示 <code>path</code> 的元素</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;parse,format&#125; = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> filePath = <span class="string">"/usr/local/bin/test.txt"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> parse_ret = parse(filePath);</span><br><span class="line"><span class="keyword">const</span> format_ret = format(parse_ret)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(parse_ret);</span><br><span class="line"><span class="built_in">console</span>.log(format_ret);</span><br></pre></td></tr></table></figure>
<ul>
<li>结果</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">&#123; <span class="attr">root</span>: <span class="string">'/'</span>,</span><br><span class="line">  dir: <span class="string">'/usr/local/bin'</span>,</span><br><span class="line">  base: <span class="string">'test.txt'</span>,</span><br><span class="line">  ext: <span class="string">'.txt'</span>,</span><br><span class="line">  name: <span class="string">'test'</span> &#125;</span><br><span class="line">/usr/local/bin\test.txt</span><br></pre></td></tr></table></figure>
<p><strong>sep、delimiter、win32、posix</strong></p>
<blockquote>
<p>和操作系统有关的东西</p>
</blockquote>
<ul>
<li><code>sep</code>：提供了平台特定的路径片段分隔符<ul>
<li><code>Windows</code> 上是<code>\</code></li>
<li><code>POSIX</code> 上是 <code>/</code></li>
</ul>
</li>
<li><code>path.win32</code>提供了 <code>path</code> 方法针对<code>Windows</code> 的实现</li>
<li><code>posix</code>   属性提供了 <code>path</code>方法针对 <code>POSIX</code> 的实现</li>
<li><code>delimiter</code> 就是 <code>&quot;:&quot;</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;sep,delimiter,win32,posix&#125; = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> filePath = <span class="string">"/usr/local/bin/test.txt"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"sep "</span>,sep);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"posix sep "</span>,posix.sep);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"win32 sep "</span>,win32.sep);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"PATH"</span>,process.env.PATH);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"delimiter "</span>,delimiter);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">sep  \</span><br><span class="line">posix sep  /</span><br><span class="line">win32 sep  \</span><br><span class="line">PATH C:\Python27\;C:\Python27\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\</span><br><span class="line">System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1<span class="number">.0</span>\;C:\Program Files\Git\b</span><br><span class="line"><span class="keyword">in</span>;C:\Program Files\nodejs;;e:\Program Files\VS Code\bin;C:\Users\Administrator\</span><br><span class="line">AppData\Roaming\npm;.;;.;;.;</span><br><span class="line">delimiter  ;</span><br></pre></td></tr></table></figure>
<p><strong>回顾小结</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 路径相关</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"__dirname"</span>, __dirname); <span class="comment">//文件夹</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"__filename"</span>, __filename); <span class="comment">//文件名</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"process.cwd()"</span>, process.cwd()); <span class="comment">//绝对路径</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"./"</span>, path.resolve()); <span class="comment">//根据相对路径取得绝对路径</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>__dirname</code>、<code>__filename</code> 总是返回文件的绝对路径</li>
<li><code>process.cwd()</code>总是返回<code>node</code>命令所在文件夹（就是用户在哪里启动的<code>node</code>脚本的路径）</li>
</ul>
<h2 id="Buffer"><a href="#Buffer" class="headerlink" title="Buffer"></a>Buffer</h2><blockquote>
<ul>
<li>buffer用来处理二进制数据流的</li>
<li>类似于整数数组，大小固定</li>
</ul>
</blockquote>
<blockquote>
<p>Buffer 类在 Node.js 中是一个全局变量，因此无需使用 require(‘buffer’).Buffer</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 创建一个长度为 10、且用 0 填充的 Buffer。</span></span><br><span class="line"><span class="keyword">const</span> buf1 = Buffer.alloc(<span class="number">10</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建一个长度为 10、且用 0x1 填充的 Buffer。 </span></span><br><span class="line"><span class="keyword">const</span> buf2 = Buffer.alloc(<span class="number">10</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建一个包含 [0x1, 0x2, 0x3] 的 Buffer。</span></span><br><span class="line"><span class="keyword">const</span> buf4 = Buffer.from([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</span><br></pre></td></tr></table></figure>
<p><strong>Buffer类常用的方法</strong></p>
<ul>
<li><code>byteLength</code> 计算字符串占了几个字节</li>
<li><code>Buffer.isBuffer</code>判断是否是<code>buffer</code>对象</li>
<li><code>Buffer.concat</code>拼接<code>Buffer</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// byteLength 计算字符串占了几个字节</span></span><br><span class="line"><span class="built_in">console</span>.log(Buffer.byteLength(<span class="string">"test"</span>)) <span class="comment">// 4 </span></span><br><span class="line"><span class="built_in">console</span>.log(Buffer.byteLength(<span class="string">"测试"</span>))  <span class="comment">// 6字节</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Buffer.isBuffer判断是否是buffer对象</span></span><br><span class="line"><span class="built_in">console</span>.log(Buffer.isBuffer(&#123;&#125;))</span><br><span class="line"><span class="built_in">console</span>.log(Buffer.isBuffer(Buffer.from([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])))</span><br><span class="line"></span><br><span class="line"><span class="comment">// Buffer.concat拼接Buffer</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf1 = Buffer.from(<span class="string">"This "</span>)</span><br><span class="line"><span class="keyword">const</span> buf2 = Buffer.from(<span class="string">"is "</span>)</span><br><span class="line"><span class="keyword">const</span> buf3 = Buffer.from(<span class="string">"a "</span>)</span><br><span class="line"><span class="keyword">const</span> buf4 = Buffer.from(<span class="string">"test "</span>)</span><br><span class="line"><span class="keyword">const</span> buf5 = Buffer.from(<span class="string">"buffer"</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf = Buffer.concat([buf1,buf2,buf3,buf4,buf5]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(buf.toString());</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">4</span></span><br><span class="line"><span class="comment">6</span></span><br><span class="line"><span class="comment">false</span></span><br><span class="line"><span class="comment">true</span></span><br><span class="line"><span class="comment">This is a test buffer</span></span><br><span class="line"><span class="comment">**/</span></span><br></pre></td></tr></table></figure>
<p><strong>Buffer实例常用方法</strong></p>
<ul>
<li><code>buf.length</code> </li>
<li><code>buf.toString()</code></li>
<li><code>buf.fill()</code> 填充一些值</li>
<li><code>buf.equals()</code> 两个<code>buffer</code>的内容是否相等</li>
<li><code>buf.indexOf()</code> 找到字符的位置</li>
<li><code>buf.copy()</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> buf = Buffer.from(<span class="string">"This is a test!"</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(buf.length); <span class="comment">// 15</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(buf.toString(<span class="string">"base64"</span>)); </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf1 = Buffer.allocUnsafe(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// buf1.fill填充一些值</span></span><br><span class="line"><span class="comment">//&lt;Buffer a0 b4 25 00 00 00 00 00 01 00&gt;</span></span><br><span class="line"><span class="comment">//&lt;Buffer a0 b4 0a 0a 0a 0a 00 00 01 00&gt;</span></span><br><span class="line"><span class="built_in">console</span>.log(buf1)</span><br><span class="line"><span class="built_in">console</span>.log(buf1.fill(<span class="number">10</span>,<span class="number">2</span>,<span class="number">6</span>)) <span class="comment">// 从第二个开始到第六个填充10</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// buf.equals两个buffer的内容是否相等</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf2 = Buffer.from(<span class="string">"test"</span>)</span><br><span class="line"><span class="keyword">const</span> buf3 = Buffer.from(<span class="string">"test"</span>)</span><br><span class="line"><span class="keyword">const</span> buf4 = Buffer.from(<span class="string">"test!"</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(buf2.equals(buf3)) <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(buf2.equals(buf4)) <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// buf.indexOf() 找到字符的位置</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(buf2.indexOf(<span class="string">"es"</span>)); <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(buf2.indexOf(<span class="string">"esa"</span>)); <span class="comment">// -1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 解决中文乱码问题</span></span><br><span class="line"><span class="keyword">const</span> StringDecoder = <span class="built_in">require</span>(<span class="string">"string_decoder"</span>).StringDecoder;</span><br><span class="line"><span class="keyword">const</span> decoder = <span class="keyword">new</span> StringDecoder(<span class="string">"utf8"</span>);</span><br><span class="line"><span class="keyword">const</span> buf5 = Buffer.from(<span class="string">"中文字符串"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 乱码</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i=<span class="number">0</span>;i&lt;buf5.length;i+=<span class="number">5</span>) &#123;</span><br><span class="line">	<span class="keyword">const</span> b = Buffer.allocUnsafe(<span class="number">5</span>);</span><br><span class="line">	buf5.copy(b,<span class="number">0</span>,i);</span><br><span class="line">	<span class="built_in">console</span>.log(b.toString());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 不在乱码</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i=<span class="number">0</span>;i&lt;buf5.length;i+=<span class="number">5</span>) &#123;</span><br><span class="line">	<span class="keyword">const</span> b = Buffer.allocUnsafe(<span class="number">5</span>);</span><br><span class="line">	buf5.copy(b,<span class="number">0</span>,i);</span><br><span class="line">	<span class="built_in">console</span>.log(decoder.write(b));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="event"><a href="#event" class="headerlink" title="event"></a>event</h2><blockquote>
<p>大多数 Node.js 核心 API 都采用惯用的异步事件驱动架构，其中某些类型的对象（触发器）会周期性地触发命名事件来调用函数对象（监听器）</p>
</blockquote>
<ul>
<li><code>eventEmitter.on()</code> 方法用于注册监听器，<code>eventEmitter.emit()</code> 方法用于触发事件</li>
<li>这和在浏览器上不同，在浏览器上靠的是用户做的一些UI的操作触发，如<code>click\hover</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">"events"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 继承EventEmitter类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CustomEvent</span> <span class="keyword">extends</span> <span class="title">EventEmitter</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> ce = <span class="keyword">new</span> CustomEvent();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 绑定一个事件名 test，完成事件处理程序</span></span><br><span class="line">ce.on(<span class="string">"test"</span>,()=&gt;&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"This is a test"</span>);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">setInterval(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="comment">// emit触发事件</span></span><br><span class="line">	ce.emit(<span class="string">"test"</span>);</span><br><span class="line">&#125;,<span class="number">500</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">"events"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 继承EventEmitter类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CustomEvent</span> <span class="keyword">extends</span> <span class="title">EventEmitter</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> ce = <span class="keyword">new</span> CustomEvent();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 绑定事件处理程序</span></span><br><span class="line">ce.on(<span class="string">"error"</span>,(err,time) =&gt; &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(err);</span><br><span class="line">	<span class="built_in">console</span>.log(time);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 给事件处理程序传递多个参数</span></span><br><span class="line">ce.emit(<span class="string">"error"</span>, <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">"出错了"</span>),<span class="built_in">Date</span>.now())</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">"events"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 继承EventEmitter类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CustomEvent</span> <span class="keyword">extends</span> <span class="title">EventEmitter</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> ce = <span class="keyword">new</span> CustomEvent();</span><br><span class="line"></span><br><span class="line"><span class="comment">// once只响应一次事件 如jQuery中的once()</span></span><br><span class="line">ce.once(<span class="string">"test"</span>,()=&gt; &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"test event once"</span>);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">setInterval(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="comment">// emit触发事件</span></span><br><span class="line">	ce.emit(<span class="string">"test"</span>);</span><br><span class="line">&#125;,<span class="number">500</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">"events"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 继承EventEmitter类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CustomEvent</span> <span class="keyword">extends</span> <span class="title">EventEmitter</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"fn1"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn2</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"fn2"</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> ce = <span class="keyword">new</span> CustomEvent();</span><br><span class="line"></span><br><span class="line">ce.on(<span class="string">"test"</span>,fn1);</span><br><span class="line">ce.on(<span class="string">"test"</span>,fn2);</span><br><span class="line"></span><br><span class="line">setInterval(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="comment">// emit触发事件</span></span><br><span class="line">	ce.emit(<span class="string">"test"</span>);</span><br><span class="line">&#125;,<span class="number">500</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// removeListener移除事件</span></span><br><span class="line">setTimeout(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="comment">// ce.removeListener("test",fn2)</span></span><br><span class="line">	ce.removeAllListeners(<span class="string">"test"</span>); <span class="comment">//移除test绑定的事件</span></span><br><span class="line">&#125;,<span class="number">1500</span>)</span><br></pre></td></tr></table></figure>
<h2 id="fs"><a href="#fs" class="headerlink" title="fs"></a>fs</h2><ul>
<li>异步方法的最后一个参数都是一个回调函数。 </li>
<li>传给回调函数的参数取决于具体方法，但回调函数的第一个参数都会保留给异常。 如果操作成功完成，则第一个参数会是 <code>null</code> 或 <code>undefined</code></li>
<li>当使用同步方法时，任何异常都会被立即抛出。 可以使用   <code>try/catch</code> 来处理异常，或让异常向上冒泡</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 读文件</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// readFile 异步操作</span></span><br><span class="line"><span class="comment">// 规范：回调函数的第一个参数都会保留给异常</span></span><br><span class="line">fs.readFile(<span class="string">"./fs.js"</span>,<span class="string">"utf8"</span>,(err,data)=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</span><br><span class="line">	<span class="built_in">console</span>.log(data);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// readFile 同步操作（同步先执行）</span></span><br><span class="line"><span class="keyword">const</span> data = fs.readFileSync(<span class="string">"./path.js"</span>,<span class="string">"utf8"</span>);</span><br><span class="line"><span class="built_in">console</span>.log(data)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 写文件</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.writeFile(<span class="string">"./write.js"</span>,<span class="string">"This is a  test"</span>,&#123;<span class="attr">encoding</span>:<span class="string">"utf8"</span>&#125;,err=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"done"</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//stat文件信息</span></span><br><span class="line">fs.stat(<span class="string">"./fs.js"</span>,(err,stats)=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span> (err) &#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">"文件不存在"</span>);</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">console</span>.log(stats.isFile()) <span class="comment">//true</span></span><br><span class="line">	<span class="built_in">console</span>.log(stats.isDirectory()) <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">console</span>.log(stats)</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">		Stats &#123;</span></span><br><span class="line"><span class="comment">		  dev: -835755091,</span></span><br><span class="line"><span class="comment">		  mode: 33206,</span></span><br><span class="line"><span class="comment">		  nlink: 1,</span></span><br><span class="line"><span class="comment">		  uid: 0,</span></span><br><span class="line"><span class="comment">		  gid: 0,</span></span><br><span class="line"><span class="comment">		  rdev: 0,</span></span><br><span class="line"><span class="comment">		  blksize: undefined,</span></span><br><span class="line"><span class="comment">		  ino: 562949953557826,</span></span><br><span class="line"><span class="comment">		  size: 216,</span></span><br><span class="line"><span class="comment">		  blocks: undefined,</span></span><br><span class="line"><span class="comment">		  atime: 2017-10-15T12:24:01.933Z,</span></span><br><span class="line"><span class="comment">		  mtime: 2017-10-15T12:49:00.341Z,</span></span><br><span class="line"><span class="comment">		  ctime: 2017-10-15T12:49:00.341Z,</span></span><br><span class="line"><span class="comment">		  birthtime: 2017-10-15T12:24:01.871Z</span></span><br><span class="line"><span class="comment">	    &#125;</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// rename修改文件名</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.rename(<span class="string">"./write.js"</span>,<span class="string">"test.js"</span>,err=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"done"</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// unlink删除文件</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.unlink(<span class="string">"./test.js"</span>,err=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"done"</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 读文件夹相关操作</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.readdir(<span class="string">"./"</span>,(err,files)=&gt;&#123;</span><br><span class="line">	<span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</span><br><span class="line">	<span class="built_in">console</span>.log(files);</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">		[ '.vscode',</span></span><br><span class="line"><span class="comment">	  'buffer.js',</span></span><br><span class="line"><span class="comment">	  'events.js',</span></span><br><span class="line"><span class="comment">	  'fs.js',</span></span><br><span class="line"><span class="comment">	  'path.js',</span></span><br><span class="line"><span class="comment">	  'test1.js',</span></span><br><span class="line"><span class="comment">	  'test2.js',</span></span><br><span class="line"><span class="comment">	  'test3.js',</span></span><br><span class="line"><span class="comment">	  'test4.js' ]</span></span><br><span class="line"><span class="comment">	 **/</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 创建文件夹</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.mkdir(<span class="string">"test"</span>,err=&gt;&#123;&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 删除文件夹</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.rmdir(<span class="string">"./test"</span>,err=&gt;&#123;&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// watch监视文件变化</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line">fs.watch(<span class="string">"./"</span>,&#123;</span><br><span class="line">	recursive: <span class="literal">true</span> <span class="comment">//是否循环递归</span></span><br><span class="line">&#125;,(eventType,filename) =&gt; &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(eventType, filename)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// readStream 流 (从一个方向流向另一个方向)</span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> rs = fs.createReadStream(<span class="string">"./fs.js"</span>);</span><br><span class="line"></span><br><span class="line">rs.pipe(process.stdout) <span class="comment">// process.stdout控制台</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// writeStream </span></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> ws = fs.createWriteStream(<span class="string">"./test5.js"</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> time = setInterval(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="keyword">const</span> num = <span class="built_in">parseInt</span>(<span class="built_in">Math</span>.random() * <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (num &lt; <span class="number">7</span>) &#123;</span><br><span class="line">		ws.write(num + <span class="string">""</span>); <span class="comment">// 需要转成字符串或者buffer才能写入</span></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		clearInterval(time);</span><br><span class="line">		ws.end();<span class="comment">// 写完数据</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;,<span class="number">200</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用事件判断是否写完数据</span></span><br><span class="line">ws.on(<span class="string">"finish"</span>, () =&gt; &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">"done!"</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="异步解决方案"><a href="#异步解决方案" class="headerlink" title="异步解决方案"></a>异步解决方案</h2><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 异步回调 Node.js v8.0.0版本增加了util.promisify函数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"><span class="keyword">const</span> promisify = <span class="built_in">require</span>(<span class="string">"util"</span>).promisify;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> read = promisify(fs.readFile); </span><br><span class="line"></span><br><span class="line">read(<span class="string">"./callback.js"</span>).then(<span class="function"><span class="params">data</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(data.toString());</span><br><span class="line">&#125;).catch(<span class="function"><span class="params">ex</span>=&gt;</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(ex)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// async awit</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">		<span class="keyword">const</span> content = awit read(<span class="string">"./callback.js"</span>);</span><br><span class="line">		<span class="built_in">console</span>.log(content.toString())</span><br><span class="line">	&#125; <span class="keyword">catch</span> (ex) &#123;</span><br><span class="line">		<span class="built_in">console</span>.log(ex)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">test()</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2017/10/15/node-env-debug/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2017/10/18/js-inherit-methods/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
